<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Template {
	
	/**
	 * List of cached variables
	 *
	 * @$path string
	 * @$data array
	 * @access protected
	 */
	protected $_cached_vars	= array();
	
	function view($path,$data='',$return=false){
		
		if($data){
			$data = $this->_object_to_array($data);
			
			if (is_array($data)){
				$this->_cached_vars = array_merge($this->_cached_vars, $data);
			}
			extract($this->_cached_vars);
		}
		
		ob_start();
		include(APPPATH.$path);
		$buffer = ob_get_contents();
		ob_end_clean();
		if($return){
			return $buffer;
		}else{
			echo $buffer;
		}
	}
	
	/**
	 * Object to Array
	 *
	 * Takes an object as input and converts the class variables to array key/vals
	 *
	 * @param	object
	 * @return	array
	 */
	protected function _object_to_array($object){
		return (is_object($object)) ? get_object_vars($object) : $object;
	}
}